<?php

class Users extends Controller {

    function __construct()
    {
        parent::Controller();
    }

    function login() {
        if ($this->session->userdata('logged_in') != TRUE) {
            $this->load->view('template/basic', array(
                    'title'		=> 'Log in',
                    'content'	=> $this->load->view('users/login', NULL, TRUE)
            ));
        }
        else {
        	redirect('');
        }
    }

    function validate_login() {
        $this->load->model('users_model');
        $user_id = $this->users_model->validate_user($this->input->post('firstname'), $this->input->post('surname'), $this->input->post('password'));
        
        if ($user_id !== FALSE) {
        	$userdata = $this->users_model->get_userdata($user_id);
        	
            $data = array(
                    'firstname'		=> $userdata['firstname'],
                    'surname'		=> $userdata['surname'],
                    'user_id'		=> $user_id,
            		'user_level'	=> $userdata['user_level'],
                    'logged_in'		=> TRUE
            );

            $this->session->set_userdata($data);

            redirect('home/main');
        }
        else {
			$this->load->view('template/basic', array(
				'title'		=> 'Log in',
				'content'	=> $this->load->view('users/login', NULL, TRUE),
				'errors'	=> array(
					array(
						'title'		=> 'Login failed',
						'message'	=> 'Login has failed. Are you sure you used the right login-information?'
					)
				)
            ));
        }
    }

    function logout() {
        $this->session->sess_destroy();
        redirect('');
    }

    function add_user()
    {
        if(!$this->session->userdata('logged_in'))
        {
            redirect('users/login');
        }
        if(!($this->session->userdata('user_level') == USER_MANAGER ||
             $this->session->userdata('user_level') == ADMIN))
        {
            redirect('home/main');
        }

        $this->form_validation->set_error_delimiters('<p class="error">', '</p>');

        if($this->form_validation->run('adduser_rules') == FALSE)
        {
            $this->load->view('template/standard', array(
                'title'     => 'Add user',
                'content'   => $this->load->view('users/add_user', NULL, TRUE)
            ));
        }
        else
        {
            $firstname = $this->input->post('firstname');
            $surname = $this->input->post('surname');
            $password = $this->input->post('password');
            $user_level = $this->input->post('user_level');
            switch($user_level)
            {
                case 'REGULAR_USER':
                    $user_level = REGULAR_USER;
                    break;
                case 'PROCESS_MANAGER':
                    $user_level = PROCESS_MANAGER;
                    break;
                case 'USER_MANAGER':
                    $user_level = USER_MANAGER;
                    break;
                case 'ADMIN':
                    $user_level = ADMIN;
                    break;
                default:
                    break;
            }

            $this->load->model('users_model');
            $this->users_model->insert_user($firstname, $surname, $password, $user_level);
            redirect('home/main');
        }
    }

    function _valid_fullname($surname, $firstname)
    {
        $this->load->model('users_model');
        $this->form_validation->set_message('_valid_fullname', 'This combination of Firsname and Surname already exists');

        if(!$this->input->post($firstname))
        {
            return FALSE;
        }

        $firstname = $this->input->post($firstname);

        return !$this->users_model->fullname_exists($firstname, $surname);
    }

    function _valid_user_level($user_level)
    {
        $this->form_validation->set_message('_valid_user_level', 'An illegal user level was selected');
        switch($user_level)
        {
            case 'REGULAR_USER':
                return TRUE;
            case 'PROCESS_MANAGER':
                return TRUE;
            case 'USER_MANAGER':
                return TRUE;
            case 'ADMIN':
                return TRUE;
            default:
                return FALSE;
        }
    }

}

?>